<template>
  <!-- <div class="content">
    <el-card class="card"> -->
  <LayoutContent>
    <div class="button">
      <div class="detail">{{ $route.query.id ? '编辑' : '新增' }}</div>
      <div>
        <el-button type="primary" @click="clickHandle('save')">保存</el-button>
        <el-button type="primary" @click="clickHandle('rest')">重置</el-button>
        <el-button @click="clickHandle('back')">返回</el-button>
      </div>
    </div>
    <div class="form">
      <el-form ref="formInstall" :model="form" status-icon :rules="rules" label-position="left" label-width="auto">
        <el-form-item label="班级名称：" prop="name">
          <el-input v-model.trim="form.name" placeholder="请输入班级名称" />
        </el-form-item>
        <el-form-item label="班级描述：" prop="des"> <el-input v-model.trim="form.des" placeholder="请输入班级描述" type="textarea" /> </el-form-item
        ><el-form-item label="班级人数：" prop="total">
          <el-input v-model.trim="form.total" placeholder="请输入班级人数" />
        </el-form-item>
        <el-form-item label="教师：" prop="teaId">
          <el-select v-model="form.teaId" placeholder="请选择" :disabled="isShowByTea">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in teacherList" :key="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
  </LayoutContent>
  <!-- </el-card>
  </div> -->
</template>

<script setup>
import { ref, reactive, onMounted, inject } from 'vue'
import { getTeacherListApi, addApi, getDetailApi } from '@/api/class/index'
import { useRoute, useRouter } from 'vue-router'

const isShowByTea = inject('isShowByTea')
const identity = inject('identity')

const rules = reactive({
  name: [{ required: true, message: '请输入班级名称', trigger: 'blur' }],
  des: [{ required: true, message: '请输入班级描述', trigger: 'blur' }],
  total: [{ required: true, message: '请输入班级人数', trigger: 'blur' }],
})

const form = ref({
  name: '',
  des: '',
  total: '',
  teaId: '',
})
const formInstall = ref(null)

const router = useRouter()
const route = useRoute()
const clickHandle = (type) => {
  switch (type) {
    case 'back':
      router.back()
      break
    case 'save':
      submit()
      break
    case 'rest':
      formInstall.value.resetFields()
      break
  }
}

const teacherList = ref([])
const getTeacherList = async () => {
  const res = await getTeacherListApi()
  if (res.code == 200) {
    teacherList.value = res.data
  } else {
    ElMessage.error(res.msg)
  }
}
onMounted(() => {
  if (!isShowByTea) {
    getTeacherList()
  }
  if (route.query.id) {
    getDetail()
  }
})

// 获取详情
const getDetail = async () => {
  const res = await getDetailApi({ id: route.query.id })
  if (res.code == 200) {
    form.value = res.data
    ElMessage.success(res.msg)
  } else {
    ElMessage.error(res.msg)
  }
}

// 提交
const submit = async () => {
  if (!formInstall.value) return
  formInstall.value.validate(async (valid) => {
    if (valid) {
      const res = await addApi(form.value, identity)
      if (res.code == 200) {
        ElMessage.success(res.msg)
      } else {
        ElMessage.error(res.msg)
      }
      router.back()
    }
  })
}
</script>

<style scoped lang="scss">
.content {
  .card {
    height: 100%;

    .button {
      width: 100%;
      display: flex;
      justify-content: space-between;
      padding-bottom: 10px;
      border-bottom: 2px solid #eee;
    }
    .form {
      margin-top: 10px;
    }
  }
}
</style>
